/********************************************************************************
* Description: 
* Author: hsqiang
* Create Date: 2023-06-21
********************************************************************************/
#ifndef NET_CONTEXT_HPP_
#define NET_CONTEXT_HPP_

#include "tcp_service.hpp"
#include "udp_service.hpp"
#include "assert_define.hpp"
#include "noncopyable.hpp"

namespace event_framework
{
    enum class TransProtocol
    {
        PROTO_TCP,
        PROTO_UDP
    };
    class NetContext : Noncopyable
    {
    public:
        NetContext(EventLoop& loop_, const EndPoint& ep, TransProtocol proto) : protocol(proto)
        {
            if (proto == TransProtocol::PROTO_TCP)
            {
                netService.reset(new TcpService(loop, ep));
            }
            else
            {
                netService.reset(new UdpService(loop, ep));
            }
            ASSERT(netService != nullptr);
        }
        bool Init() { netService->Init(); }
        bool Fini() { netService->Fini(); }
        bool IsTcp() { return protocol == TransProtocol::PROTO_TCP; }
        bool IsUdp() { return protocol == TransProtocol::PROTO_UDP; }
        TcpService& GetTcpService()
        {
            ASSERT(IsTcp());
            auto service = std::dynamic_pointer_cast<TcpService>(netService);
            ASSERT(service != nullptr);
            return *service;
        }
        UdpService& GetUdpService()
        {
            ASSERT(IsUdp());
            auto service = std::dynamic_pointer_cast<TcpService>(netService);
            ASSERT(service != nullptr);
            return *service;
        }
    private:
        TransProtocol protocol;
        NetServicePtr netService;
    };
}

#endif